/**
 * 组织结构员工列表控制器
 */
import $_fn from '~plugins'
import './index.less'
export default ['$scope','$rootScope','$location','$ui','http','$state',function($scope,$rootScope,$location,$ui,http,$state) {
    $scope.$_fn = $_fn
    $scope.data = {}
    $scope.err = {}
    $scope.loanType = {}
    $scope.equalResult = {}
    $scope.result = {}
    $scope.option = {}
    $scope.loanState = 3 //默认信贷 //借款类型(1.车贷 2房贷 3信贷)
    $scope.loanType.xin = true
	//四级按钮
	$scope.tabMenu = $_fn.getCurrMenu($location.path())
    console.log('tabMenu',$scope.tabMenu)
    $scope.subMenu = $scope.tabMenu.children[0].children
    console.log($scope.subMenu)
    $scope.search = $location.search()
    $scope.state = $scope.search.state || 1

    //警告提示
    $scope.errorToast = function(text) {
        $ui.toast({text:text,type:'warning',position:'center',appendTo:'.wp-container'});
    }
    $scope.init = function(){
        if($scope.state == 1){
            http.post('appListInit.do',{},function (data) {
                let _data = data.data
                if(data.errcode > 0){
                    $scope.$apply(function () {
                        $scope.dataList = _data

                    })
                }else{
                    $scope.errorToast(data.errmsg)
                }
            })
        }else if($scope.state == 2){
            http.post('appDetail.do',{id:$scope.search.id},function (data) {
                let _data = data.data
                if(data.errcode > 0){
                    $scope.$apply(function () {
                        $scope.data = _data
                        $scope.option.period = _data.applyTermList
                    })
                }else{
                    $scope.errorToast(data.errmsg)
                }
            })
        }

    }
    $scope.init()

    $scope.linkApp = function (item) {
        console.log('linkApp',item)
        $state.go('operation.loanapp',{state:2,id:item.id},{reload:true})
    }
    $scope.loanSelect = function(par, state){
        $_fn.applyShow(par, $scope.loanType) 
        $scope.loanState = state
    }
    /**
     * 去除报错高亮
     */
    $scope.remlight = function () {
      for (var i in $scope.err) {
        $scope.err[i] = false
      }
    }
    $scope.resureCalc = function(){
        if($scope.data.loan_money==null || $scope.data.loan_money==undefined){
            $scope.err.loan_money = true
            $scope.errorToast('请填写贷款金额')
            return
        }
        if($scope.data.at_id==null || $scope.data.at_id==undefined){
            $scope.err.at_id = true
            $scope.errorToast('请选择期数')
            return
        }
        if($scope.data.interest==null || $scope.data.interest==undefined){
            $scope.err.interest = true
            $scope.errorToast('请填写利息')
            return
        }
        $scope.result.loan_money = $scope.data.loan_money
        for(let i=0; i<$scope.option.period.length;i++){
            if($scope.option.period[i].id == $scope.data.at_id){
                $scope.result.num = $scope.option.period[i].num
            }
        }

        let merPayInterest = $scope.result.loan_money*$scope.data.interest/100
        //总支付利息
        $scope.result.pay_interest= merPayInterest*$scope.result.num
        //等额等息每月还款
        $scope.equalResult.month_pay= (merPayInterest+($scope.result.loan_money/$scope.result.num)).toFixed(2)
        //先息后本每月还款
        $scope.result.month_pay= merPayInterest
        //总还款
        $scope.result.total= parseInt($scope.data.loan_money)+parseInt($scope.result.pay_interest)

        http.post('addAppLog.do',{app_id:$scope.search.id,version:$scope.data.app.version,type:4},function (data) {
            if(data.errcode > 0){
            }else{
                $scope.errorToast(data.errmsg)
            }
        })
    }
	//四级按钮页面跳转方法
	$scope.operation = function (path,item) {
		console.log('111')
        var _path = path.split('@')
        let params = JSON.stringify(item)
        if(_path[0] == 'link'){
            console.log(path)
            $state.go(_path[1], item)
        }
        if(_path[0] == 'alert'){
            $ui.modal({content:"<"+_path[1]+" params="+params+"></"+_path[1]+">"})
        }
    }
}]
